package com.chengwang.bookstory.controller;

import com.chengwang.bookstory.mapper.ManagerMapper;
import com.chengwang.bookstory.pojo.Manager;
import com.chengwang.bookstory.service.Imple.ManagerServiceImple;
import com.chengwang.bookstory.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.List;

@Controller
@RequestMapping("manager")
public class ManagerController {
    @Autowired
    private ManagerService managerService;
    @Autowired
    private ManagerMapper managerMapper;
    //返回首页
    @RequestMapping("/toIndex")
    public String toIndex(){
        return "index";
    }
    //退出登录
    @RequestMapping("/toLeave")
    public String toLeave(HttpSession session){
        Enumeration<String> attributeNames = session.getAttributeNames();
        if(attributeNames.hasMoreElements()){
            session.removeAttribute(attributeNames.nextElement().toString());
        }
        return "redirect:/login.html";
    }
    

//    @RequestMapping("/switchManager")
//    public String switchManager(){
//        return "/regist.html";
//    }
    @RequestMapping("/mLogin")
    public ModelAndView mLogin(Manager manager, HttpSession session, HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        boolean b = managerService.mlogin(manager);
        if (b){
            session.setAttribute("manager",manager);
            mv.addObject("manager",manager);
            mv.setViewName("manager.html");
            return mv;
        }else {
            mv.addObject("mes", "用户名或密码错误");
            mv.setViewName("redirect:/login.html");
            return mv;
        }
    }
}
